﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.HTTPHelpers;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

namespace Mengsk.ProxyDelivery.Service.Spider.Go2
{
    public class Go2Spider : SpiderBase, ICloneable
    {
        private const string url = "http://www.go2.cn/supplier";

        public override string Name
        {
            get { return "Go2 抓取"; }
        }

        public override SpiderType SpiderType
        {
            get { return SpiderType.GO2; }
        }

        public Go2Spider()
        {
            this.URL = url;
        }


        private string[] FormatColors(string str)
        {
            string[] colors = str.Split(",，，.。。\\、＼:：： ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            return colors;
        }

        private string[] FormatSizes(string str)
        {
            string[] sizes = str.Split(",，，:：：.。。\\、＼ ".ToArray(), StringSplitOptions.RemoveEmptyEntries);
            return sizes;
        }

        private ShoesType FormatType(string type)
        {
            if (type == "凉鞋")
            {
                return ShoesType.LIANGXIE;
            }
            if (type == "低帮鞋")
            {
                return ShoesType.DIBANGXIE;
            }
            if (type == "高帮鞋")
            {
                return ShoesType.GAOBANGXIE;
            }
            if (type == "拖鞋")
            {
                return ShoesType.TUOXIE;
            }
            if (type == "靴子")
            {
                return ShoesType.XUEZI;
            }
            if (type == "男鞋")
            {
                return ShoesType.NANXIE;
            }
            if (type == "帆布鞋")
            {
                return ShoesType.FANBUXIE;
            }
            if (type == "雨鞋")
            {
                return ShoesType.YUXIE;
            }
            return ShoesType.OTHER;
        }

        public override Shoes[] GetVendorShoes(Vendor vendor)
        {
            string html = MSHttpRestful.DoGetWithRetry(5, vendor.HomePage, Encoding.UTF8);
            Dictionary<string, string> headers = new Dictionary<string, string>();

            headers.Add("X-Requested-With", "XMLHttpRequest");
            headers.Add("Accept-Language", "en-US,en;q=0.5");
            headers.Add("Accept-Encoding", "gzip, deflate");

            //查询价格
            string mark = "/product/info";
            int startIndex = html.IndexOf(mark);
            if (startIndex < 0)
            {
                Debug.WriteLine("下载厂家商品出错,无法获取产品信息" + vendor.Name + "," + vendor.Id + ", " + vendor.HomePage);
                return new Shoes[0];
            }
            int endIndex = html.IndexOf(';', startIndex);
            string priceQuery = html.Substring(startIndex, endIndex - startIndex - 1);
            string priceUrl = string.Format("{0}{1}", vendor.HomePage, priceQuery);

            //该厂家没有商品
            if (priceQuery[priceQuery.Length - 1] == '/')
            {
                Debug.WriteLine("下载厂家商品出错,该厂家没有商品" + vendor.Name + "," + vendor.Id + ", " + vendor.HomePage);
                return new Shoes[0];
            }

            string priceJson = MSHttpRestful.DoGetWithRetry(5, priceUrl, Encoding.UTF8);
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Go2Price[]));
            Go2Price[] go2Prices = ds.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(priceJson), false)) as Go2Price[];
            //
            XDocument xDoc = GetContentAsXml(html, "id=\"productlist\"", "div");
            XElement xProducts = xDoc.Root.Elements("ul").First() as XElement;
            List<Shoes> shoess = new List<Shoes>();
            var items = xProducts.Elements("li").ToArray();

            foreach (XElement xe in items)
            {
                Shoes shoes = new Shoes { CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Enabled = true };
                shoes.Number = (xe.Elements("strong").First().FirstNode as XElement).Value.Trim().Split('&')[1].Trim();
                shoes.Url = (xe.Elements("strong").First().FirstNode as XElement).Attribute("href").Value.Trim();
                shoes.Image = xe.Element("span").Element("table").Element("tr").Element("td").Element("a").Element("img").Attribute("src").Value;

                int index = shoes.Url.IndexOf("?");
                if (index >= 0)
                {
                    shoes.Url = shoes.Url.Substring(0, index);
                }
                string priceClass = xe.Element("label").Attribute("class").Value.Trim();
                float price = go2Prices.First(obj => priceClass.StartsWith(obj.id)).price;

                shoes.Price = price;
                try
                {
                    //下载详情页面
                    string detailHtml = MSHttpRestful.DoGetWithRetry(5, shoes.Url, Encoding.UTF8);
                    XDocument xDetail = GetContentAsXml(detailHtml, "id=\"propsul\"", "ul");
                    //解析类型
                    string typeContent = GetContentString(detailHtml, "id=\"biinfo\"", "div");
                    int indexType = typeContent.IndexOf("产品类型");
                    int strongIndex = typeContent.IndexOf("<strong>", indexType);
                    int strongEndIndex = typeContent.IndexOf("</strong>", strongIndex);
                    shoes.Type = FormatType(typeContent.Substring(strongIndex + "<strong>".Length, strongEndIndex - strongIndex - "<strong>".Length).Trim());
                    if (shoes.Type == ShoesType.OTHER)
                    {
                        Debug.WriteLine("厂家：" + vendor.Name + " 商品：" + typeContent.Substring(strongIndex + "<strong>".Length, strongEndIndex - strongIndex - "<strong>".Length).Trim() + "  " + shoes.Url + "已被跳过");
                        continue;
                    }

                    //获取颜色
                    XElement xColor = xDetail.Root.Elements("li").FirstOrDefault(obj => obj.Value.Contains("颜色"));
                    string[] colors = null, sizes = null;
                    if (xColor != null)
                    {
                        colors = this.FormatColors(xColor.Value.Split(':', '：')[1].Trim());
                    }
                    else
                    {
                        colors = new string[0];
                    }

                    //下载尺码
                    int sizeStartIndex = detailHtml.IndexOf("/ajax/product/stats/");
                    int sizeEndIndex = detailHtml.IndexOf(';', sizeStartIndex);
                    string sizeQuery = string.Format("http://www.go2.cn{1}", vendor.HomePage, detailHtml.Substring(sizeStartIndex, sizeEndIndex - sizeStartIndex - 1));
                    string sizeContent = MSHttpRestful.DoGetWithRetry(5, sizeQuery, Encoding.UTF8, "application/json, text/javascript, */*; q=0.01", shoes.Url, null, headers, null);
                    DataContractJsonSerializer sizeSer = new DataContractJsonSerializer(typeof(Go2Size));
                    Go2Size go2Size = sizeSer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(sizeContent), false)) as Go2Size;
                    sizes = this.FormatSizes(go2Size.size);

                    //生成sku
                    if (sizes.Length == 0) { sizes = new string[] { "34,35,36,37,38,39" }; }
                    if (colors.Length == 0) { colors = new string[] { "默认" }; }

                    foreach (var color in colors)
                    {
                        foreach (var size in sizes)
                        {
                            ShoesSku sku = new ShoesSku { Color = color, Size = size, Edtion = "", Price = price, OffLinePrice = price, ShoesId = 0 };
                            shoes.Skus.Add(sku);
                        }
                    }
                    shoes.UpdateEnabled = true;
                    shoess.Add(shoes);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("下载出错:" + vendor.Name + "  " + shoes.Number + "  " + shoes.Url + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                    this.AppendErrorMessage("下载出错:" + vendor.Name + "  " + shoes.Number + "  " + shoes.Url);
                }
            }

            return shoess.ToArray();
        }

        private Dictionary<ShoesType, List<Shoes>> ConvertToDictionary(Shoes[] goodss)
        {
            Dictionary<ShoesType, List<Shoes>> dic = new Dictionary<ShoesType, List<Shoes>>();

            foreach (var item in goodss)
            {
                if (dic.ContainsKey(item.Type) == false)
                {
                    dic[item.Type] = new List<Shoes>();
                }
                dic[item.Type].Add(item);
            }
            foreach (var gs in dic.Values)
            {
                var items = gs.OrderBy(obj => obj.Number).ToArray();
                gs.Clear();
                gs.AddRange(items);
            }
            return dic;
        }

        private void AppendGoods(string title, Shoes goods)
        {
            string colors = String.Empty;
            string sizes = String.Empty;

            if (goods.Skus.Count > 0)
            {
                colors = goods.Skus.Select(obj => obj.Color).Distinct().Aggregate((sum, current) => sum + "," + current);
                sizes = goods.Skus.Select(obj => obj.Size).Distinct().Aggregate((sum, current) => sum + "," + current);
            }
            float price = 0;
            if (goods.Skus.Count > 0)
            {
                price = goods.Skus[0].Price;
            }
            else
            {
                price = goods.Price;
            }
            string content = string.Format("{0} {1} {2} {3} {4} {5} {6}元", title, goods.VendorId, goods.Number, goods.Type, colors, sizes, price);
            this.OnMessage(content);
        }

        private void ProcessGoods(Shoes[] shoessInDB, Shoes[] shoessOnline)
        {
            int iDB = 0, iOnline = 0;
            ShoesService shoesService = ServiceContainer.GetService<ShoesService>();

            for (; iDB < shoessInDB.Length && iOnline < shoessOnline.Length; )
            {
                shoessOnline[iOnline].UpdateTime = DateTime.Now;
                int compare = string.Compare(shoessInDB[iDB].Number, shoessOnline[iOnline].Number);
                if (compare > 0)
                {
                    shoesService.InsertWithSku(shoessOnline[iOnline]);
                    this.AppendGoods("插入", shoessOnline[iOnline]);
                    iOnline++;
                }
                else if (compare == 0)
                {
                    if (shoessInDB[iDB].Enabled == false)
                    {
                        if (shoessInDB[iDB].UpdateEnabled == true)
                        {
                            shoessInDB[iDB].Enabled = true;
                            shoesService.Update(shoessInDB[iDB]);
                            this.AppendGoods("已启用", shoessOnline[iOnline]);
                        }
                        else
                        {
                            this.AppendGoods("商品不允许更新", shoessOnline[iOnline]);
                        }
                    }
                    else if ((int)(shoessInDB[iDB].Price * 100) != ((int)(shoessOnline[iOnline].Price * 100)))
                    {
                        if (shoessInDB[iDB].UpdateEnabled == true)
                        {
                            shoessInDB[iDB].Price = shoessOnline[iOnline].Price;
                            shoesService.UpdatePrice(shoessInDB[iDB].Id, shoessInDB[iDB].Price);
                            this.AppendGoods("改价格", shoessOnline[iOnline]);
                        }
                        else
                        {
                            this.AppendGoods("商品不允许更新", shoessOnline[iOnline]);
                        }
                    }
                    else
                    {
                        this.AppendGoods("未更新", shoessInDB[iDB]);
                    }
                    iDB++;
                    iOnline++;
                }
                else
                {
                    // shoessInDB[iDB].Enabled = false;
                    // shoessInDB[iDB].UpdateTime = DateTime.Now;
                    //shoesService.Update(shoessInDB[iDB]);
                    this.AppendGoods("未禁用", shoessInDB[iDB]);
                    iDB++;
                }
            }

            for (; iDB < shoessInDB.Length; iDB++)
            {
                //shoessInDB[iDB].Enabled = false;
                //shoessInDB[iDB].UpdateTime = DateTime.Now;
                //shoesService.Update(shoessInDB[iDB]);
                this.AppendGoods("未禁用", shoessInDB[iDB]);
            }

            for (; iOnline < shoessOnline.Length; iOnline++)
            {
                shoessOnline[iOnline].UpdateTime = DateTime.Now;
                shoesService.InsertWithSku(shoessOnline[iOnline]);
                this.AppendGoods("插入", shoessOnline[iOnline]);
            }
        }

        protected override void DoSycGoodss(Vendor[] vendors, ShoesService shoesService)
        {
            int i = 0;
            foreach (var vendor in vendors)
            {
                if (this.isStop)
                {
                    break;
                }
                i++;
                try
                {
                    //从数据获取所有数据
                    this.OnMessage(string.Format("正在从数据库读取 {0} 的商品", vendor.Name));
                    Shoes[] goodssInDB = shoesService.Get(vendor.Id).OrderBy(obj => obj.Number).ToArray();
                    this.OnMessage(string.Format("正在从网络下载 {0} 的商品", vendor.Name));
                    Shoes[] goodssOnline = this.GetVendorShoes(vendor).OrderBy(obj => obj.Number).ToArray();
                    foreach (var item in goodssOnline)
                    {
                        item.VendorId = vendor.Id;
                    }
                    Dictionary<ShoesType, List<Shoes>> gDb = ConvertToDictionary(goodssInDB);
                    Dictionary<ShoesType, List<Shoes>> gOnline = ConvertToDictionary(goodssOnline);

                    ShoesType[] dbTypes = gDb.Keys.ToArray().OrderBy(obj => obj).ToArray();
                    ShoesType[] onlineTypes = gOnline.Keys.ToArray().OrderBy(obj => obj).ToArray();
                    int iDB = 0, iOnline = 0;
                    Shoes[] emptyGoodss = new Shoes[0];

                    for (; iDB < dbTypes.Length && iOnline < onlineTypes.Length; )
                    {
                        int compare = (int)dbTypes[iDB] - (int)onlineTypes[iOnline];
                        if (compare > 0)
                        {
                            this.ProcessGoods(emptyGoodss, gOnline[onlineTypes[iOnline]].ToArray());
                            iOnline++;
                        }
                        else if (compare == 0)
                        {
                            this.ProcessGoods(gDb[dbTypes[iDB]].ToArray(), gOnline[onlineTypes[iOnline]].ToArray());
                            iOnline++;
                            iDB++;
                        }
                        else
                        {
                            this.ProcessGoods(gDb[dbTypes[iDB]].ToArray(), emptyGoodss);
                            iDB++;
                        }
                    }

                    for (; iDB < dbTypes.Length; iDB++)
                    {
                        this.ProcessGoods(gDb[dbTypes[iDB]].ToArray(), emptyGoodss);
                    }

                    for (; iOnline < onlineTypes.Length; iOnline++)
                    {
                        this.ProcessGoods(emptyGoodss, gOnline[onlineTypes[iOnline]].ToArray());
                    }

                    this.OnMessage("已更新厂家:" + vendor.Name + "的所有的商品");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                    this.AppendErrorMessage("更新厂家商品出错：" + vendor.Name + ex.Message);
                }
                finally
                {
                    this.OnProgress(i, vendors.Length);
                }
            }
        }

        /// <summary>
        /// 已重写，更新厂家信息
        /// </summary>
        /// <param name="vendorService"></param>
        protected override void DoSycVendor(VendorService vendorService)
        {
            int totalPage = 10;
            int currentPage = 1;
            int count = 0;
            Dictionary<string, string> pa = new Dictionary<string, string>();
            List<Vendor> vendors = new List<Vendor>();
            while (currentPage <= totalPage && this.isStop == false)
            {
                //从网络读取html
                string pageUrl = string.Format("{0}/0-0-{1}-2-all.go", url, currentPage);
                string html = MSHttpRestful.PostUrlEncodeBodyReturnString(pageUrl, new Dictionary<string, string>());

                //生成html文档
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                //获取总页数
                var changePageNode = doc.DocumentNode.SelectNodes("//a[@class='pagenum']").Last();
                totalPage = int.Parse(changePageNode.InnerText.Trim());

                //解析厂家
                var vendorNodes = doc.DocumentNode.SelectNodes("//div[@class='store']");
                foreach (var xe in vendorNodes)
                {
                    if (this.isStop)
                    {
                        break;
                    }
                    Vendor vendor = new Vendor { Spider = (int)SpiderType, Address = "", Enabled = true };
                    try
                    {
                        //等级
                        try
                        {
                            var levelValueNode = xe.SelectSingleNode("div[@class='storegrade']").SelectSingleNode("label");
                            string levelValueText = levelValueNode.Attributes["title"].Value.Trim();
                            string[] levelValueTexts = levelValueText.Split(":： ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            vendor.LevelValue = int.Parse(levelValueTexts[levelValueTexts.Length - 1].Trim());
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.Message);
                            vendor.LevelValue = 0;
                        }
                        var detailNode = xe.SelectSingleNode("div[@class='message']");
                        //主页
                        vendor.HomePage = detailNode.SelectSingleNode("div[@class='sto_01']/a[@class='webaddr']/span").InnerText.Trim();
                        //名称
                        vendor.Name = detailNode.SelectSingleNode("p[@class='storename']|p[@class='storename_b']").InnerText.Trim();
                        //电话
                        vendor.Phone = detailNode.SelectNodes("p[@class='detmsg']/font")[0].FirstChild.InnerText.Trim();
                        //类似
                        vendor.Type = detailNode.SelectNodes("p[@class='detmsg']")[1].InnerText.Trim().Contains("生产加工") ? VendorType.Self : VendorType.Proxy;
                        //QQ
                        vendor.QQ = string.Join("", detailNode.SelectNodes("p[@class='detmsg']")[2].SelectNodes("font")[1].FirstChild.InnerText.Trim().Where(c => Char.IsDigit(c)));
                        //入驻时间
                        try
                        {
                            var createTimeNode = detailNode.SelectNodes("p[@class='detmsg']")[3].SelectNodes("span[@class='number']").Last();
                            vendor.CreateTime = DateTime.Now.AddDays(int.Parse(createTimeNode.InnerText.Trim()) * 30 * -1);
                        }
                        catch
                        {
                            Debug.WriteLine("解析厂家入驻时间失败:" + vendor.HomePage);
                            vendor.CreateTime = DateTime.Now.Date;
                        }
                        //拿货地址
                        vendor.MarketAddress = detailNode.SelectSingleNode("p[@class='detaddr']").InnerText.Trim().Split(":：".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1];

                        if (vendors.Any(obj => obj.HomePage.Equals(vendor.HomePage)))
                        {
                            Debug.WriteLine("当前抓取队列中已包含该厂家，将跳过:" + vendor.Name + "  " + vendor.HomePage);
                            continue;
                        }
                        count++;
                        IList<Vendor> al = vendorService.GetByName(vendor.Name);
                        if (al == null || al.Count < 1)
                        {
                            al = vendorService.GetAllByField("homePage", vendor.HomePage, 0, 0).Datas;
                            if (al != null && al.Count > 0)
                            {
                                al[0].PingyingName = "";
                            }
                        }
                        if (al != null && al.Count > 0)
                        {
                            vendor.Enabled = true;
                            vendor.Id = al[0].Id;
                            vendor.PingyingName = al[0].PingyingName;
                            if (vendorService.Match(vendor, al[0]) == false)
                            {
                                vendorService.Update(vendor);
                                this.OnMessage(string.Format("已更新 {0}  {1} ", count, vendor.Name));
                            }
                            else
                            {
                                this.OnMessage(string.Format("未更新 {0}  {1} ", count, vendor.Name));
                            }
                        }
                        else
                        {
                            vendorService.Insert(vendor);
                            this.OnMessage(string.Format("已增加 {0}  {1} ", count, vendor.Name));
                        }
                        vendors.Add(vendor);
                    }
                    catch (Exception ex)
                    {
                        this.AppendErrorMessage("出错错误");
                        Debug.WriteLine(ex);
                        throw ex;
                    }
                }
                currentPage++;
            }
        }

        public object Clone()
        {
            Go2Spider spider = new Go2Spider
            {
                URL = url,
                isStop = true,
            };
            return spider;
        }

        public override Shoes[] GetVendorShoes(Vendor vendor, string number)
        {
            string html = MSHttpRestful.DoGetWithRetry(5, vendor.HomePage, Encoding.UTF8);
            Dictionary<string, string> headers = new Dictionary<string, string>();

            headers.Add("X-Requested-With", "XMLHttpRequest");
            headers.Add("Accept-Language", "en-US,en;q=0.5");
            headers.Add("Accept-Encoding", "gzip, deflate");

            //查询价格
            string mark = "/product/info";
            int startIndex = html.IndexOf(mark);
            if (startIndex < 0)
            {
                Debug.WriteLine("下载厂家商品出错,无法获取产品信息" + vendor.Name + "," + vendor.Id + ", " + vendor.HomePage);
                return new Shoes[0];
            }
            int endIndex = html.IndexOf(';', startIndex);
            string priceQuery = html.Substring(startIndex, endIndex - startIndex - 1);
            string priceUrl = string.Format("{0}{1}", vendor.HomePage, priceQuery);

            //该厂家没有商品
            if (priceQuery[priceQuery.Length - 1] == '/')
            {
                Debug.WriteLine("下载厂家商品出错,该厂家没有商品" + vendor.Name + "," + vendor.Id + ", " + vendor.HomePage);
                return new Shoes[0];
            }

            string priceJson = MSHttpRestful.DoGetWithRetry(5, priceUrl, Encoding.UTF8);
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Go2Price[]));
            Go2Price[] go2Prices = ds.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(priceJson), false)) as Go2Price[];
            //
            XDocument xDoc = GetContentAsXml(html, "id=\"productlist\"", "div");
            XElement xProducts = xDoc.Root.Elements("ul").First() as XElement;
            List<Shoes> shoess = new List<Shoes>();
            var items = xProducts.Elements("li").ToArray();

            foreach (XElement xe in items)
            {
                Shoes shoes = new Shoes { CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Enabled = true };
                shoes.Number = (xe.Elements("strong").First().FirstNode as XElement).Value.Trim().Split('&')[1].Trim();

                if (number.Equals(shoes.Number, StringComparison.OrdinalIgnoreCase) == false)
                {
                    continue;
                }

                shoes.Url = (xe.Elements("strong").First().FirstNode as XElement).Attribute("href").Value.Trim();
                shoes.Image = xe.Element("span").Element("table").Element("tr").Element("td").Element("a").Element("img").Attribute("src").Value;

                int index = shoes.Url.IndexOf("?");
                if (index >= 0)
                {
                    shoes.Url = shoes.Url.Substring(0, index);
                }
                string priceClass = xe.Element("label").Attribute("class").Value.Trim();
                float price = go2Prices.First(obj => priceClass.StartsWith(obj.id)).price;

                shoes.Price = price;
                try
                {
                    //下载详情页面
                    string detailHtml = MSHttpRestful.DoGetWithRetry(5, shoes.Url, Encoding.UTF8);
                    XDocument xDetail = GetContentAsXml(detailHtml, "id=\"propsul\"", "ul");
                    //解析类型
                    string typeContent = GetContentString(detailHtml, "id=\"biinfo\"", "div");
                    int indexType = typeContent.IndexOf("产品类型");
                    int strongIndex = typeContent.IndexOf("<strong>", indexType);
                    int strongEndIndex = typeContent.IndexOf("</strong>", strongIndex);
                    shoes.Type = FormatType(typeContent.Substring(strongIndex + "<strong>".Length, strongEndIndex - strongIndex - "<strong>".Length).Trim());
                    if (shoes.Type == ShoesType.OTHER)
                    {
                        Debug.WriteLine("厂家：" + vendor.Name + " 商品：" + typeContent.Substring(strongIndex + "<strong>".Length, strongEndIndex - strongIndex - "<strong>".Length).Trim() + "  " + shoes.Url + "已被跳过");
                        continue;
                    }

                    //获取颜色
                    XElement xColor = xDetail.Root.Elements("li").FirstOrDefault(obj => obj.Value.Contains("颜色"));
                    string[] colors = null, sizes = null;
                    if (xColor != null)
                    {
                        colors = this.FormatColors(xColor.Value.Split(':', '：')[1].Trim());
                    }
                    else
                    {
                        colors = new string[0];
                    }

                    //下载尺码
                    int sizeStartIndex = detailHtml.IndexOf("/ajax/product/stats/");
                    int sizeEndIndex = detailHtml.IndexOf(';', sizeStartIndex);
                    string sizeQuery = string.Format("http://www.go2.cn{1}", vendor.HomePage, detailHtml.Substring(sizeStartIndex, sizeEndIndex - sizeStartIndex - 1));
                    string sizeContent = MSHttpRestful.DoGetWithRetry(5, sizeQuery, Encoding.UTF8, "application/json, text/javascript, */*; q=0.01", shoes.Url, null, headers, null);
                    DataContractJsonSerializer sizeSer = new DataContractJsonSerializer(typeof(Go2Size));
                    Go2Size go2Size = sizeSer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(sizeContent), false)) as Go2Size;
                    sizes = this.FormatSizes(go2Size.size);

                    //生成sku
                    if (sizes.Length == 0) { sizes = new string[] { "默认尺码" }; }
                    if (colors.Length == 0) { colors = new string[] { "默认颜色" }; }

                    foreach (var color in colors)
                    {
                        foreach (var size in sizes)
                        {
                            ShoesSku sku = new ShoesSku { Color = color, Size = size, Edtion = "", Price = price, OffLinePrice = price, ShoesId = 0 };
                            shoes.Skus.Add(sku);
                        }
                    }
                    shoes.UpdateEnabled = true;
                    shoess.Add(shoes);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("下载出错:" + vendor.Name + "  " + shoes.Number + "  " + shoes.Url + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                    this.AppendErrorMessage("下载出错:" + vendor.Name + "  " + shoes.Number + "  " + shoes.Url);
                }
            }

            return shoess.ToArray();
        }

    }
}
